package org.lc.oauth.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Getter
@Schema(description = "用户详细信息")
public class LcUserDetails extends User {
    /**
     * 客户端ID
     */
    @Schema(description = "客户端ID")
    private final String clientId;
    /**
     * 用户ID
     */
    @Schema(description = "用户ID")
    private final String userId;

    /**
     * 用户昵称
     */
    @Schema(description = "用户昵称")
    private final String nickName;

    /**
     * 是否是超级管理员
     */
    @Schema(description = "是否是超级管理员")
    private final boolean superAdmin;

    /**
     * 用户角色
     */
    @Schema(description = "用户角色")
    private final List<String> roles;

    /**
     * 用户前端权限
     */
    @Schema(description = "用户前端权限")
    private final List<String> acls;
    /**
     * 用户后端权限
     */
    @Schema(description = "用户后端权限")
    private final List<String> apis;

    /**
     * 设置SpringSecurity需要的role
     */
    private static List<GrantedAuthority> getPrivileges(final Collection<String> roles) {
        final List<GrantedAuthority> authorities = new ArrayList<>();

        for (final String role : roles) {
            authorities.add(new SimpleGrantedAuthority(role));
        }
        return authorities;
    }


    public LcUserDetails(String clientId, String id, String password, String account, String name, Boolean superAdmin, List<String> roles, List<String> acls, List<String> apis) {
        super(account, password, LcUserDetails.getPrivileges(new ArrayList<>()));
        this.clientId = clientId;
        this.userId = id;
        this.nickName = name;
        this.superAdmin = superAdmin;
        this.acls = acls;
        this.roles = roles;
        this.apis = apis;
    }
}
